package com.etoak.controller;

import com.etoak.common.utils.DateUtils;
import com.etoak.common.utils.uuid.IdUtils;
import com.etoak.entity.Draft;
import com.etoak.service.ICheckListDraftService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * CheckList库草稿箱Controller
 * @Author zhixuebin
 * @Date 2022/5/16
 */
@RestController
@RequestMapping("/goertek/checklist/draft")
public class CheckListDraftController extends BaseController {

    @Autowired
    ICheckListDraftService checkListDraftService;

    /*仅对当前登录用户返回草稿,无列表展示接口*/
    /**
     * 保存草稿
     * @param draft
     * @return
     */
    @PostMapping("/addDraft")
    public AjaxResult addDraft(@RequestBody Draft draft){
        //设置主键
        String id = IdUtils.simpleUUID();
        draft.setId(id);
        //设置应用场景
        String[] applicationScenarios = draft.getApplicationScenariosList();
        if(!StringUtils.isNull(applicationScenarios)){
            ArrayList result = new ArrayList();
            result.addAll(Arrays.asList(applicationScenarios));
            draft.setApplicationScenarios(result.toString());
        }
        //获取当前用户
        //SysUser user = ;
        //draft.setCreateBy(user);
        draft.setCreateTime(DateUtils.getNowDate());
        draft.setDelStatus(0);
        int i = 0;
        i = checkListDraftService.addDraft(draft);
        if(i > 0){
            return AjaxResult.success("草稿保存成功");
        }else{
            return AjaxResult.error("草稿保存失败");
        }
    }

    /**
     * 获取草稿记录(用于用户知识新增前判断)
     * @param draft
     * @return
     */
    @GetMapping("/getDraft")
    public AjaxResult getDraft(Draft draft){
        //根据当前用户搜索对应草稿记录
        //String user = ;
        Draft result = checkListDraftService.getDraft(draft);
        if(result != null){
            return AjaxResult.success(result);
        }else{
            return AjaxResult.error("当前用户暂无草稿~");
        }
    }

    /**
     * 逻辑删除草稿记录
     * @param ids
     * @return
     */
    @GetMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable String[] ids){
        int i = 0;
        for(String id : ids){
            Draft draft = checkListDraftService.getDraftById(id);
            draft.setDelStatus(1);
            checkListDraftService.updateDraft(draft);
            i++;
        }
        if(i > 0){
            return AjaxResult.success("已删除"+ i +"条记录");
        }else{
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 删除草稿
     * @param id
     * @return
     */
    @DeleteMapping("/deleteDraft")
    public AjaxResult deleteDraft(@RequestParam("id") String id){
        int i = checkListDraftService.deleteDraft(id);
        if(i > 0){
            return AjaxResult.success("删除成功");
        }else{
            return AjaxResult.error("删除失败");
        }

    }

    /**
     * 编辑修改草稿
     * @param draft
     * @return
     */
    @PutMapping("/updateDraft")
    public AjaxResult updateDraft(@RequestBody Draft draft){
        //查询对应记录
        Draft search = checkListDraftService.getDraftById(draft.getId());
        String[] applicationScenarios = draft.getApplicationScenariosList();
        if(!StringUtils.isNull(applicationScenarios)){
            ArrayList result = new ArrayList();
            result.addAll(Arrays.asList(applicationScenarios));
            draft.setApplicationScenarios(result.toString());
        }
        int i = 0;
        if (!StringUtils.isNull(search)) {
            i = checkListDraftService.updateDraft(draft);
        }
        if(i > 0){
            return AjaxResult.success("修改成功");
        }else{
            return AjaxResult.error("修改失败");
        }
    }



}
